﻿//Persist Helper for storage data 
//author: Alexis 2011-06-25
//blog: http://www.cnblogs.com/alexis/


using System;
using System.Net;
using System.Windows;
using System.IO.IsolatedStorage;
using System.IO;

namespace Alexis.WindowsPhone
{
    public class PersistHelper
    {
        public static string lastClickTime = "last_time";
        public static DateTime defaultTime = new DateTime(1977, 1, 1);

        public static T LoadSetttingFromStorage<T>(string Key,T defaultValue)
        {
            T ObjToLoad = default(T);

            if (IsolatedStorageSettings.ApplicationSettings.Contains(Key))
            {
                ObjToLoad = (T)IsolatedStorageSettings.ApplicationSettings[Key];
            }
            else
            {
                ObjToLoad = defaultValue;
            }

            return ObjToLoad;
        }

        public static void SaveSettingToStorage(string Key, object Setting)
        {
            if (!IsolatedStorageSettings.ApplicationSettings.Contains(Key))
            {
                IsolatedStorageSettings.ApplicationSettings.Add(Key, Setting);
            }
            else
            {
                IsolatedStorageSettings.ApplicationSettings[Key] = Setting;
            }

            IsolatedStorageSettings.ApplicationSettings.Save();
        }

        public static bool IsSettingPersisted(string Key)
        {
            return IsolatedStorageSettings.ApplicationSettings.Contains(Key);
        }
    }
}
